<?php
require_once 'admin'.EXT;
class ta_phothong extends admin{
	
	function __construct(){
		parent::__construct();
	}
	
	function index()
	{
		$this->load->helper('download');
		$rows = $this->db->select("id,correct,question,vquestion,sound,img,answer,vcorrect")->from("ta_phothong")->get()->result();
		
		if($_SERVER["REQUEST_METHOD"] == "POST")
		{
			foreach($rows as $k => $v){
				unset($rows[$k]->id);
			}
			$tmp = array();
			$tmp["bai"] = $rows;
			$data = json_encode($tmp);
			$name = 'words.json';
			
			force_download($name, $data);
		}
		
		$this->data["rows"] = $rows;
		$this->load->view("ta_phothong/index", $this->data);
	}
	
	function add()
	{
		$id = isset($_GET["id"]) && $_GET["id"] > 0 ? $_GET["id"] : 0;
		
		$row = array();
		if($id > 0){
			$row = $this->db->select()->from("ta_phothong")->where("id", $id)->get()->row();
		}
		
		if($row){
			$this->data["row"] = $row;
		}
		
		if($_SERVER["REQUEST_METHOD"] == "POST"){
			unset($_POST["btn_add"]);
			if($row){
				$this->db->where("id", $id);
				$this->db->update("ta_phothong", $_POST);
				
			}
			else
				$this->db->insert("ta_phothong", $_POST);
			
			redirect("ta_phothong/index");
		}
		$this->load->view("ta_phothong/add", $this->data);
	}
}